package leetcode_1001_1100;

import java.util.Arrays;

public class LeeCode_1093 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(sampleStats(new int[]{0, 1, 3, 4})));
    }

    private static double[] sampleStats(int[] count) {
        int n = count.length;
        double min = 0, max = 0;
        for (int i = 0; i < n; i++) {
            if (count[i] != 0) {
                min = i;
                break;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            if (count[i] != 0) {
                max = i;
                break;
            }
        }
        long sum = 0;
        int c = 0;
        for (int i = 0; i < n; i++) {
            sum += (long) i * count[i];
            c += count[i];
        }
        double mean = (double) sum / c;
        double mid = 0;
        if  (c % 2 == 1) {
            int m = c / 2 + 1, now = 0;
            for (int i = 0; i < n; i++) {
                now += count[i];
                if (now >= m) {
                    mid = i;
                    break;
                }
            }
        }else {
            int n1 = 0, n2 = 0, m1 = c / 2, m2 = m1 + 1, now = 0;
            boolean f1 = true, f2 = true;
            for (int i = 0; i < n; i++) {
                now += count[i];
                if (f1 && now >= m1){
                    n1 = i;
                    f1 = false;
                }
                if (f2 && now >= m2){
                    n2 = i;
                    f2 = false;
                }
            }
            mid = ((double) n1 + n2) / 2;
        }
        int maxC = 0, mode = 0;
        for (int i = 0; i < n; i++) {
            if (count[i] > maxC){
                maxC = count[i];
                mode = i;
            }
        }
        return new double[]{min, max, mean, mid, mode};
    }
}
